/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import model.PhongBan;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author MRDO
 */
public class PhongBanDao {
    private static Session session;
    
    public PhongBanDao(){
        SessionFactory factory = HibernateUtil.getSessionFactory();
        this.session = factory.getCurrentSession();
    }
    
    /*
     * insertPhongBan
     */
    public boolean insertPhongBan(String maPhongBan, String tenPhongBan){
        try {
            session.beginTransaction();
            
            PhongBan phongBan = new PhongBan(maPhongBan, tenPhongBan);
            
            session.save(phongBan);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
    
    /*
     * updatePhongBan
     */
    public boolean updatePhongBan(String maPhongBan, String tenPhongBan){
        try {
            session.beginTransaction();
            
            PhongBan phongBan = (PhongBan) session.get(PhongBan.class, maPhongBan);
            phongBan.setPhongBan(tenPhongBan);
            
            session.update(phongBan);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
    
    /*
     * deletePhongBan
     */
    public boolean deletePhongBan(String maPhongBan){
        try {
            session.beginTransaction();
            
            PhongBan phongBan = (PhongBan) session.get(PhongBan.class, maPhongBan);
            
            session.delete(phongBan);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
}
